import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.SocketException;

	public class UserInput implements Runnable{

		DataInputStream in;
		String drawable;
		Player player;
		boolean run;
		InetAddress address;
		boolean left = false;
		boolean right = false;
		boolean up = false;
		boolean down = false;
		boolean space = false;
		boolean canCreateAttack0 = true;
		boolean B = false;
		boolean canBlast = true;
		public UserInput(Player player, DataInputStream in)
		{
			this.in = in;
			this.player = player;
			drawable = "null";
			run = true;
			this.address = address;
		}
		/**
		 * handles user input
		 */
		@Override
		public void run() {
			int breaks = 0;
			while(run)
			{
				try{
					String temp = in.readUTF();
					if(temp != null)
					{	
						drawable = temp;
					}
					else
					{
						drawable = "";
					}
					if(drawable != null)
					{
						decode(drawable);
					}
				} catch (IOException e) {
					if(breaks >= 600)
					{
						drawable = "";
						run = false;
						try {
							in.close();
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						player.stop();
						Server.gui.display(" disconnected");
						player.canStart(true);
					}
					breaks ++;
				}
			}
		}
		public void decode(String str)
		{
			
			String ans = str + "";
			left = false;
			right = false;
			up = false;
			down = false;
			space = false;
			B = false;
			while(ans.length() > 4 && ans.contains("\n"))
			{
				if(str.startsWith("Action: "))
				{
					String temp = ans.substring(8,ans.indexOf("\n"));
					if(temp.contains("SPACE"))
					{
						space = true;
					}
					else if(temp.contains("B"))
					{
						B = true;
					}
					if(temp.contains("LEFT"))
					{
						left = true;
					}
					if(temp.contains("RIGHT"))
					{
						right = true;
					}
					if(temp.contains("UP"))
					{
						up = true;
					}
					if(temp.contains("DOWN"))
					{
						down = true;
					}
				}
				else if(str.startsWith("Chat: "))
				{
					player.sendChat(str.substring(6,str.indexOf("\n")));
				}
				else if(str.startsWith("Character Number: "))
				{
					System.out.println(Integer.parseInt(str.substring(18,20)));
					player.setCharacterNumber(Integer.parseInt(str.substring(18,20)));
				}
				//set to read next line
				try{
				if(ans.indexOf("\n") + 1 >= ans.length())
				{
					ans = "";
				}
				else
				{
					ans = ans.substring(ans.indexOf("\n") + 1);
				}
				}
				catch(Exception e)
				{
					Server.gui.display(e.toString());
					ans = "";
				}
			}
			//movement
			float direction = player.getDirection();
			Player.Speeds speed = Player.Speeds.MOVEMENT;
			if(up == true)
			{
				if(left == true)
				{
					direction = ((float)Math.toRadians(225));
				}
				else if(right == true)
				{
					direction = ((float)Math.toRadians(-45));
				}
				else
				{
					direction = ((float)Math.toRadians(-90));
				}
			}
			else if(down == true)
			{
				if(left == true)
				{
					direction =((float)Math.toRadians(135));
				}
				else if(right == true)
				{
					direction = ((float)Math.toRadians(45));
				}
				else
				{
					direction = ((float)Math.toRadians(90));
				}
			}
			else if(right == true)
			{
				direction = ((float)Math.toRadians(0));
			}
			else if(left == true)
			{
				direction = ((float)Math.toRadians(180));
			}
			else
			{
				speed = Player.Speeds.STOP;
			}
			player.sendMovementPacket(new MovementPacket(true,direction,speed));
			//create energy attacks
			if(space == true)
			{
				if(B == false && canBlast == true)
				{
					canCreateAttack0 = false;
					player.engAtt0();
				}
			}
			else
			{ 
				if(canCreateAttack0 == false)
				{
					canCreateAttack0 = true;
					player.releaseEngAtt0();
				}
				else if(B == true)
				{
					player.blastAtt();
					canBlast = false;
				}
				else
				{
					if(canBlast == false)
					{
						canBlast = true;
						player.releaseBlast();
					}
				}
			}
		}
		/**
		 * checkst to see if this thread is still running
		 * @return run
		 */
		public boolean isRunning()
		{
			return run;
		}
		/**
		 * returns the ip address of this user
		 * @return
		 */
		public InetAddress getInetAddress()
		{
			return address;
		}
		/**
		 * sets the input stream
		 * @param in
		 */
		public void setInputStream(DataInputStream in)
		{
			this.in = in;
		}
		/**
		 * stops this thread, meaning it's no longer looking for user input
		 */
		public void stop()
		{
			run = false;
		}
		/**
		 * starts this thread, meaning it is now ready to recieve user input
		 */
		public void start()
		{
			run = true;
		} 
	}